package cn.edu.zjut.mina.service.impl;

import java.util.Date;
import cn.edu.zjut.mina.util.ResultInfoConstUtils;

import org.apache.mina.core.session.IoSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.edu.zjut.mina.bean.RoomInfo;
import cn.edu.zjut.mina.bean.StandardFormat;
import cn.edu.zjut.mina.interf.IRoomInfo;
import cn.edu.zjut.mina.po.MessageRecord;
import cn.edu.zjut.mina.protocal.JMessageProtocalRes;
import cn.edu.zjut.mina.protocal.ProtocalRegulation;
import cn.edu.zjut.mina.repository.MessageRecordRepository;
import cn.edu.zjut.mina.service.IMessageService;
import cn.edu.zjut.mina.service.INotificationService;
import cn.edu.zjut.mina.util.GsonUtils;
import cn.edu.zjut.mina.util.UserSessionMap;
@Service("messageService")
public class MessageServiceImpl implements IMessageService{

	/**
	 * 用于存放用户信息的集合
	 */
	private final UserSessionMap userSessionMap = UserSessionMap.getInstance();
	
	@Qualifier("notificationService")
	@Autowired
	private INotificationService notificationService;
	
	@Autowired
	private MessageRecordRepository messageRecordRepository;
	
	public JMessageProtocalRes sendMessage(StandardFormat inJson,
			IoSession session) {
		// TODO Auto-generated method stub
		System.out.println("execute --SendMessage()-- method.");
		JMessageProtocalRes res = new JMessageProtocalRes();
		StandardFormat outJson = new StandardFormat();
		String datas;
		IRoomInfo roomInfo = userSessionMap.get(inJson.roomId);
		
		if (roomInfo == null) {
			outJson.userId = inJson.userId;
			outJson.resultInfo = ResultInfoConstUtils.SENDMESSAGE_ROOMNULLINFO;
			outJson.result = false;
		} else {
			synchronized(roomInfo) {
				for (int index = 0; index < roomInfo.getActualParticipation(); index++) {
					if (roomInfo.getUserSessions().get(index).user.getUserId().equals(inJson.userId)) {
						roomInfo.getUserSessions().get(index).user.setSendMessage(1);
						roomInfo.getUserSessions().get(index).user.setMessage(inJson.message);
						break;
					}
				}
				notificationService.notificationSendMessage(inJson.roomId, inJson.userId);
			}
			messagerecordSave(inJson, roomInfo);//消息记录保存
			return null;
		}
		
		res.setResultCode(ProtocalRegulation.PREPAREINITIATIVE);
		datas = GsonUtils.beanToJson(outJson).trim();
		res.setContent(datas);
		return null;
	}
	
	/**
	 * 消息结果保存
	 * @param inJson
	 * @param roomInfo
	 */
	@Transactional(rollbackFor=Exception.class)
	private void messagerecordSave (StandardFormat inJson, IRoomInfo roomInfo) {
		MessageRecord messagerecord = new MessageRecord();
		messagerecord.setMessage(inJson.message);
		messagerecord.setMessage_time(new Date());
		messagerecord.setRoomId(inJson.roomId);
		messagerecord.setSendmessageuserId(inJson.userId);
		int m = 0;
		for (int index = 0; index < roomInfo.getActualParticipation(); index++) {
			if (!roomInfo.getUserSessions().get(index).user.getUserId().equals(inJson.userId)) {
				if (m == 0) {
					messagerecord.setOther1(roomInfo.getUserSessions().get(index).user.getUserId());
					m++;
				} else if (m == 1) {
					messagerecord.setOther2(roomInfo.getUserSessions().get(index).user.getUserId());
					m++;
				} else if (m == 2) {
					messagerecord.setOther3(roomInfo.getUserSessions().get(index).user.getUserId());
					m++;
				}  else if (m == 3) {
					messagerecord.setOther4(roomInfo.getUserSessions().get(index).user.getUserId());
					m++;
				}
				
			}
			
		}
		messageRecordRepository.save(messagerecord);
	}

}
